﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.OracleClient;
using Xiaov.SmartWeb.Application.SupCan;

namespace Xiaov.SmartWeb.Application.HXZB
{
    public partial class ZyzbIndexData : System.Web.UI.Page
    {
        protected string nf = DateTime.Now.Year.ToString();
        protected string yf = (DateTime.Now.Month - 1).ToString();
        protected string dwbm = "HB";       

        protected void Page_Load(object sender, EventArgs e)
        {
            nf = Request.QueryString["nf"];
            yf = Request.QueryString["yf"];
            dwbm = Request.QueryString["dwbm"];

            InitTable();       
        }

        protected void InitTable()
        {            
            string sbSql = "";
            DataTable dt = new DataTable();
            dt.Columns.Add("zbmc");
            dt.Columns.Add("bysj");
            dt.Columns.Add("bnlj");
            dt.Columns.Add("qntqlj");
            dt.Columns.Add("zbbm");
            #region 工业总产值
            sbSql = @"select round(bysj) bysj,round(bnlj) bnlj,round(qntqlj) qntqlj,t.zbbm from tg03 t
                WHERE zbbm='TG030003' and nf={1} and yf={0} and dwbm='{2}' ";  //round(to_char(sysdate,'MM'))
            OracleDataReader dr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(string.Format(sbSql, yf, nf, "HB"));
            if (dr.Read())
            {
                DataRow drow = dt.NewRow();
                drow[0] = "工业总产值";
                drow[1] = dr["bysj"].ToString();
                drow[2] = dr["bnlj"].ToString();
                drow[3] = dr["qntqlj"].ToString();
                drow[4] = dr["zbbm"].ToString(); ;
                dt.Rows.Add(drow);
            }
            else
            {
                DataRow drow = dt.NewRow();
                drow[0] = "工业总产值";
                drow[1] = "";
                drow[2] = "";
                drow[3] = "";
                drow[4] = "0";
                dt.Rows.Add(drow);
            }
            dr.Close();
            #endregion

            #region 工业销售产值
            sbSql = @"select round(bysj) bysj,round(bnlj) bnlj,round(qntqlj) qntqlj,t.zbbm from tg03 t
                WHERE zbbm='TG030004' and nf={1} and yf={0} and dwbm='{2}' ";
            dr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(string.Format(sbSql, yf, nf, "HB"));
            if (dr.Read())
            {
                DataRow drow = dt.NewRow();
                drow[0] = "工业销售产值";
                drow[1] = dr["bysj"].ToString();
                drow[2] = dr["bnlj"].ToString();
                drow[3] = dr["qntqlj"].ToString();
                drow[4] = dr["zbbm"].ToString();
                dt.Rows.Add(drow);
            }
            else
            {
                DataRow drow = dt.NewRow();
                drow[0] = "工业销售产值";
                drow[1] = "";
                drow[2] = "";
                drow[3] = "";
                drow[4] = "0";
                dt.Rows.Add(drow);
            }
            dr.Close();
            #endregion

            #region 原油产量
            sbSql = @"select round(bysj) bysj,round(bnlj) bnlj,round(qntqlj) qntqlj,t.zbbm from tg03 t
                WHERE zbbm='TG030008' and nf={1} and yf={0} and dwbm='{2}' ";
            dr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(string.Format(sbSql, yf, nf, "HB"));
            if (dr.Read())
            {
                DataRow drow = dt.NewRow();
                drow[0] = "原油产量";
                drow[1] = dr["bysj"].ToString();
                drow[2] = dr["bnlj"].ToString();
                drow[3] = dr["qntqlj"].ToString();
                drow[4] = dr["zbbm"].ToString(); ;
                dt.Rows.Add(drow);
            }
            else
            {
                DataRow drow = dt.NewRow();
                drow[0] = "原油产量";
                drow[1] = "";
                drow[2] = "";
                drow[3] = "";
                drow[4] = "0";
                dt.Rows.Add(drow);
            }
            dr.Close();
            #endregion

            #region 天然气产量
            sbSql = @"select round(bysj) bysj,round(bnlj) bnlj,round(qntqlj) qntqlj,t.zbbm from tg03 t
                WHERE zbbm='TG030026' and nf={1} and yf={0} and dwbm='{2}' ";
            dr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(string.Format(sbSql, yf, nf, "HB"));
            if (dr.Read())
            {
                DataRow drow = dt.NewRow();
                drow[0] = "天然气产量";
                drow[1] = dr["bysj"].ToString();
                drow[2] = dr["bnlj"].ToString();
                drow[3] = dr["qntqlj"].ToString();
                drow[4] = dr["zbbm"].ToString();
                dt.Rows.Add(drow);
            }
            else
            {
                DataRow drow = dt.NewRow();
                drow[0] = "天然气产量";
                drow[1] = "";
                drow[2] = "";
                drow[3] = "";
                drow[4] = "0";
                dt.Rows.Add(drow);
            }
            dr.Close();
            #endregion

            #region 利润总额
            sbSql = @"select round(t.bnlj-nvl(s.bnlj,0)) bysj,round(t.bnlj) bnlj,round(t.qntqlj) qntqlj,t.zbbm from tg82 t,
                (select * from tg82 where zbbm='TG820013'and nf={1} and yf={0}-1 and dwbm='{2}') s 
                WHERE t.zbbm='TG820013' and t.nf={1} and t.yf={0} and t.dwbm='{2}' and t.zbbm=s.zbbm(+) ";
            dr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(string.Format(sbSql, yf, nf, "HB"));
            if (dr.Read())
            {
                DataRow drow = dt.NewRow();
                drow[0] = "利润总额";
                drow[1] = dr["bysj"].ToString();
                drow[2] = dr["bnlj"].ToString();
                drow[3] = dr["qntqlj"].ToString();
                drow[4] = dr["zbbm"].ToString();
                dt.Rows.Add(drow);
            }
            else
            {
                DataRow drow = dt.NewRow();
                drow[0] = "利润总额";
                drow[1] = "";
                drow[2] = "";
                drow[3] = "";
                drow[4] = "0";
                dt.Rows.Add(drow);
            }
            dr.Close();
            #endregion

            #region 原油递减率
            sbSql = @"select round(bysj) bysj,round(bnlj) bnlj,round(qntqlj) qntqlj,t.zbbm from tg07 t
                WHERE zbbm='TG070031' and nf={1} and yf={0} and dwbm='{2}' ";
            dr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(string.Format(sbSql, yf, nf, "HB"));
            if (dr.Read())
            {
                DataRow drow = dt.NewRow();
                drow[0] = "原油递减率";
                drow[1] = dr["bysj"].ToString();
                drow[2] = dr["bnlj"].ToString();
                drow[3] = dr["qntqlj"].ToString();
                drow[4] = dr["zbbm"].ToString();
                dt.Rows.Add(drow);
            }
            else
            {
                DataRow drow = dt.NewRow();
                drow[0] = "原油递减率";
                drow[1] = "";
                drow[2] = "";
                drow[3] = "";
                drow[4] = "0";
                dt.Rows.Add(drow);
            }
            dr.Close();
            #endregion

            #region 能源合计
            sbSql = @"select round(t.xHhj-nvl(s.xHhj,0)) bysj,round(t.XHHJ) bnlj,round(r.XHHJ) qntqlj,t.zbbm from NH61 t,
                (select * from NH61 where zbbm='NH6111'and nf={1} and yf={0}-1 and dwbm='{2}') s,
                (select * from NH61 where zbbm='NH6111'and nf={1}-1 and yf={0} and dwbm='{2}') r 
                WHERE t.zbbm='NH6111' and t.nf={1} and t.yf={0} and t.dwbm='{2}' and t.zbbm=s.zbbm(+) and t.zbbm=r.zbbm(+)";
            dr = (OracleDataReader)Maticsoft.DBUtility.DbHelperOra.ExecuteReader(string.Format(sbSql, yf, nf, "HB"));
            if (dr.Read())
            {
                DataRow drow = dt.NewRow();
                drow[0] = "能源消耗合计";
                drow[1] = dr["bysj"].ToString();
                drow[2] = dr["bnlj"].ToString();
                drow[3] = dr["qntqlj"].ToString();
                drow[4] = dr["zbbm"].ToString();
                dt.Rows.Add(drow);
            }
            else
            {
                DataRow drow = dt.NewRow();
                drow[0] = "能源消耗合计";
                drow[1] = "";
                drow[2] = "";
                drow[3] = "";
                drow[4] = "0";
                dt.Rows.Add(drow);
            }
            dr.Close();
            #endregion
            
            #region 转换成XML
            string dataString = ConfigurationManager.AppSettings["ZyzbIndex_Column"];
            ConvertToXml.Convert(dt, dataString, 0, dt.Rows.Count);          
            #endregion
        }
    }
}